package com.dz.scrm.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by hai.huang on 2018/5/22.
 * 离线数据缓存类
 */

public class SharedPreferencesUtil {
    public static SharedPreferences preferences;


    public final static String TOKEN = "token";// token

    public final static String NAME = "name";// name

    public final static String PWD = "pwd";// pwd

    public final static String RegistrationID = "RegistrationID";// 推送设备ID

    public final static String ISWIFI = "ISWIFI";// pwd

    public final static String PHONE_NUM = "PHONE_NUM";//

    public final static String LOGIN_INFO = "LOGIN_INFO";//

    public final static String COMPANY_INFO = "COMPANY_INFO";//

    public final static String CALL_PHONE_NUM = "CALL_PHONE_NUM";//

    public static void initPreferences(Context context) {
        if (preferences == null) {
            preferences = PreferenceManager.getDefaultSharedPreferences(context);
        }
    }

    /***
     * 存储信息
     * @param key
     * @param ver
     */
    public static void saveInfo(String key, String ver) {
        try {
            if (preferences != null) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString(key, ver);
                editor.apply();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void saveStatus(String key, boolean sp) {
        try {
            if (preferences != null) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean(key, sp);
                editor.apply();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean getStatus(String key) {
        boolean info = false;
        try {
            if (!isEmty(key) && preferences != null) {
                info = preferences.getBoolean(key, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /***
     * 存储信息
     * @param key
     */
    public static String getInfo(String key) {
        String info = "";
        try {
            if (!isEmty(key) && preferences != null) {
                info = preferences.getString(key, "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    public static void remove(String key) {
        try {
            if (preferences != null) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.remove(key);
                editor.apply();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param sp
     * @return true表示是空 false不是空
     */
    public static boolean isEmty(String sp) {
        boolean result = false;
        if (sp == null || sp.equals("") || sp.equals("null") || sp.equals("[]")) {
            result = true;
        }
        return result;
    }
}
